<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/9/18
  Time: 19:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="服务项目管理"/>
</jsp:include>

<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>


<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.admin.sidebar.server.project.management"/></h4>

        <div class="form-group has-success has-feedback" style="float: right;margin-right: 40px;">
            <input type="text" placeholder='<spring:message code="label.admin.search"/>' class="form-control" id="inputSuccess2"
                   style="border-radius:5px;border-color:#aabfcb">
            <span class="glyphicon glyphicon-search form-control-feedback"></span>
        </div>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body" style="padding: 0">
                <table class="table" id="service_list">
                    <thead>
                    <tr>
                        <th><spring:message code="label.sequence.number"/></th>
                        <th><spring:message code="label.admin.user.list.name"/></th>
                        <th><spring:message code="label.tenant.service.description"/></th>
                        <th><spring:message code="label.admin.user.list.department"/></th>
                        <th><spring:message code="label.admin.user.list.operation"/></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        var table = $('#service_list').DataTable({
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            deferRender: true,
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            aLengthMenu: [15],
            ajax: {
                url: '/admin/api/v1/service/listByDataTable',
                dataSrc: 'data'
            },
            columns: [
                {
                    data: null, width: '100px'
                },
                {
                    data: 'service_name'
                },
                {
                    data: 'service_desc'
                },
                {
                    data: 'enabled',
                    "render": function (data, type, full, meta) {
                        if (data == 0) {
                            return '<spring:message code="label.tenant.service.not.available"/>';
                        } else if (data == 1) {
                            return '<spring:message code="label.tenant.service.available"/>'
                        } else {
                            return '<spring:message code="label.tenant.data.abnormal"/>';
                        }
                    }
                },
                {
                    data: 'service_id',
                    "render": function (data, type, full, meta) {
                        var divString = '<div class="sie-btn-group">' +
                                '<a href="javascript:;" onclick="serviceEnabled(' + data.toString() + ',1)"><i class="sie-btn-start"></i><span><spring:message code="label.user.enabled"/></span></a>' +
                                '<a href="javascript:;" onclick="serviceEnabled(' + data + ',0)"><i class="sie-btn-stop"></i><span><spring:message code="label.user.disabled"/></span></a>' +
                                '</div>';
                        return divString;
                    }
                }
            ],
            "language": {url: '/lang/datatable.chs.json'}
        });
        $('#inputSuccess2').bind('keyup', function (e) {
            table.search(this.value).draw();
        });

        table.on('draw.dt', function () {
            table.column(0, {
                search: 'applied',
                order: 'applied'
            }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        });
    });

    var serviceEnabled = function (data, enabled) {
        $.ajax({
            url: "/admin/api/v1/service/setEnabled",
            type: "post",
            data: {
                serviceId: data,
                enabled: enabled
            },
            success: function () {
                swal({
                    title: '<spring:message code="label.success.operation"/>',
                    text: "",
                    type: "success",
                    showCancelButton: false,
                    showConfirmButton: true,
                    confirmButtonText: '<spring:message code="label.admin.confirm"/>',
                    animation: "slide-from-top"
                }, function () {
                    $('#service_list').DataTable().ajax.reload();
                });
            },
            fail: function () {
                swal("Error!", "", "error");
                $('#service_list').DataTable().ajax.reload();
            }
        })
    }
</script>
